package com.neuedu.carsystem.action;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.carsystem.model.Car;
import com.neuedu.carsystem.service.CarService;

@Controller
@RequestMapping("/car")
public class CarAction {
	
	@Autowired
	private CarService carService;
	
	//分页查询所有
	@RequestMapping("/findCar")
	public String findCarAll(HttpServletRequest request,HttpServletResponse response){
		int page=Integer.parseInt(request.getParameter("page"));
		int rows = 7;
		//总分页数
		int pageCount;
		Map<String, Object> pageMap=new HashMap<String, Object>();
		//从第几条数据开始查询
		int total = carService.countCar();
		System.out.println("总记录数："+total);
		if(page<1) {
			page=1;
		}
		if(total%2==0) {
			pageCount =total/rows;
		}else {
			pageCount =total/rows+1;
		}
		if(page>pageCount) {
			page=pageCount;
		}
		pageMap.put("startPage", (page-1)*rows);
		//每页查询多少条记录
		pageMap.put("endPage", rows);
		//进行分页查询
		List<Car> carList = carService.findCarAll(pageMap);
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("carList", carList);
		request.setAttribute("total", total);
		return "car_list";
		//return "redirect:/carlist.jsp";
	}
	
	//按ID查询
	@RequestMapping("/findByIdCar")
	public String findById(HttpServletRequest request,HttpServletResponse response){
			int carno =Integer.parseInt(request.getParameter("id"));
			System.out.println("修改id为："+carno);
			Car car= carService.findById(carno);
			request.setAttribute("carList", car);
			return "car_edit";
	}
	
	//添加
	@RequestMapping("/addCar")
	@ResponseBody
	public String addCar(HttpServletRequest request,HttpServletResponse response){
			String carnumber =request.getParameter("carnumber");
			String carsb =	request.getParameter("carsb");
			String carcolor = request.getParameter("carcolor");
			String cartype = request.getParameter("cartype");
			System.out.println(carnumber+","+carsb+carcolor+","+cartype+","+cartype);
			Car car = new Car(0,carnumber,carsb,carcolor,cartype);
			try {
				int ret = carService.addCar(car);
			}catch (Exception e) {
			}
			return "{success:true}";
	}
	//修改
	@RequestMapping("/updateCar")
	public String updateCar(HttpServletRequest request,HttpServletResponse response){
			int carno =Integer.parseInt(request.getParameter("carno"));
			System.out.println("修改id为："+carno);
			String carnumber =request.getParameter("carnumber");
			String carsb =	request.getParameter("carsb");
			String carcolor = request.getParameter("carcolor");
			String cartype = request.getParameter("cartype");
			System.out.println(carno +" "+carnumber+" "+carsb+" "+ carcolor+" "+cartype);
			//int ret = carService.updateCar(carno);
			
		return null;
	}
	//删除
	@RequestMapping("/deleteCar")
	public String deleteCar(Integer id,HttpServletRequest request,HttpServletResponse response){
			int ret = carService.deleteCar(id);
		return null;
	}
	
	
}
